//
//  UsercenterVC.m
//  SSC
//
//  Created by 红沙尘 on 17/6/8.
//  Copyright © 2017年 红沙尘. All rights reserved.
//

#import "UsercenterVC.h"
#import "AppDelegate.h"
#import "LoginVC.h"
#import "TouzhujiluVC.h"
#import "ZhongjiangjiluVC.h"
#import "ChongzhijiluVC.h"
#import "TixianjiluVC.h"
#import "BangkaVC.h"
#import "ZijinmimaVC.h"
#import "DenglumimaVC.h"
#import "KaihuzhongxinvC.h"
#import "ZhuihaoTabBarController.h"
#import "TixianVC.h"
#import "ChongzhiVC.h"
#import "TeamManager.h"
#import "JiebangVC.h"

@interface UsercenterVC ()
{
    NSArray *dataArray;
    __weak IBOutlet NSLayoutConstraint *lineA;
    __weak IBOutlet NSLayoutConstraint *lineB;
    
    __weak IBOutlet UILabel *moneyLabel;
    __weak IBOutlet UIButton *_logoutBtn;
    __weak IBOutlet UIImageView *headerview;
    __weak IBOutlet UILabel *nameLabel;
    __weak IBOutlet UIView *moneyView;
    IBOutlet UIView *tableViewHaderView;
}

@end

@implementation UsercenterVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setTitle:@"个人中心"];
    self.navigationItem.leftBarButtonItem = CUSTOMVIEW([UIView new]);
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    nameLabel.text = [Userinfo standardUserinfo].username;
    
    [[Userinfo standardUserinfo] userMoney:^(NSString *money) {
       
        WeakSelf
         [weakSelf updateMoney:money];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - IBAction

- (IBAction)gotoChongzhi {
    [self.navigationController pushViewController:[[ChongzhiVC alloc] init] animated:YES];
}

- (IBAction)gotoTixian {
    [self.navigationController pushViewController:[[TixianVC alloc] init] animated:YES];
}

- (IBAction)logout {
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"退出登录"
                                                                   message:@"是否退出当前账号"
                                                            preferredStyle:UIAlertControllerStyleAlert];
    
    [alert addAction:[UIAlertAction actionWithTitle:@"取消"
                                              style:UIAlertActionStyleCancel
                                            handler:^(UIAlertAction * _Nonnull action) {
        
    }]];
    
    [alert addAction:[UIAlertAction actionWithTitle:@"退出"
                                              style:UIAlertActionStyleDestructive
                                            handler:^(UIAlertAction * _Nonnull action)
    {
        AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
        [app gotoLogin];
    }]];
    
    [self presentViewController:alert animated:YES completion:nil];
}

#pragma mark - tableview

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.001;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 12;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return dataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [dataArray[section] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UCCell"];
    
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:0 reuseIdentifier:@"UCCell"];
        cell.textLabel.font = [MTool fontOfSize:14];
        cell.textLabel.textColor = Color333333;
    
        cell.accessoryView = [[UIImageView alloc] initWithImage:LoadImage(@"home_5")];
        
        UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 39, ScreenWidth, LineHeight)];
        lineView.backgroundColor = COLOR_CCCCCC;
        lineView.tag = 23452;
        [cell addSubview:lineView];
    }
    
    NSArray *array = dataArray[indexPath.section];
    
    NSDictionary *dict = array[indexPath.row];
    cell.textLabel.text = dict[@"title"];
    cell.imageView.image = LoadImage(dict[@"image"]);
    
    UIView *lineView = [cell viewWithTag:23452];
    
    if (indexPath.row == array.count - 1) {
        lineView.hidden = YES;
    } else {
        lineView.hidden = NO;
    }
    
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    UIViewController *viewController;
    
    switch (indexPath.section) {
        case 0:
        {
            switch (indexPath.row) {
                case 0:viewController = [[TouzhujiluVC alloc] init];break;
                case 1:viewController = [[ZhuihaoTabBarController alloc] init];break;
                case 2:viewController = [[ZhongjiangjiluVC alloc] init];break;
                case 3:viewController = [[ChongzhijiluVC alloc] init];break;
                case 4:viewController = [[TixianjiluVC alloc] init];break;
            }
        }
            break;
        case 1:
        {
            switch (indexPath.row) {
                case 0:viewController = [[BangkaVC alloc] init];break;
                case 1:viewController = [[JiebangVC alloc] init];break;
                case 2:viewController = [[ZijinmimaVC alloc] init];break;
                case 3:viewController = [[DenglumimaVC alloc] init];break;
            }
        }
            break;
        case 2:
        {
            switch (indexPath.row) {
                case 0:viewController = [[TeamManager alloc] init];break;
            }
        }
            break;
        case 3:
        {
            switch (indexPath.row) {
                case 0:viewController = [[KaihuzhongxinvC alloc] init];break;
            }
        }
            break;
        default:
            break;
    }
    
    if (viewController)
    [self.navigationController pushViewController:viewController animated:YES];
}

#pragma mark setup

- (void)setupData {
    dataArray = @[@[@{@"title":@"投注记录",@"image":@"uc0"},
                    @{@"title":@"追号记录",@"image":@"uc0_1"},
                    @{@"title":@"中奖记录",@"image":@"uc1"},
                    @{@"title":@"充值记录",@"image":@"uc2"},
                    @{@"title":@"提现记录",@"image":@"uc3"}],
                  
                  @[@{@"title":@"绑定银行卡",@"image":@"uc4"},
                    @{@"title":@"解绑银行卡",@"image":@"uc4"},
                    @{@"title":@"资金密码",@"image":@"uc5"},
                    @{@"title":@"登录密码",@"image":@"uc6"}],
                  
                  @[@{@"title":@"团队管理",@"image":@"uc7"}],
                    
                  @[@{@"title":@"开户中心",@"image":@"uc7"}]
                  ];
}

- (void)setupUI {
    _logoutBtn.layer.cornerRadius  = 2;
    _logoutBtn.layer.masksToBounds = YES;
    
    mainTableView.rowHeight = 40;
    mainTableView.estimatedRowHeight = 40;
    
    headerview.layer.cornerRadius = 25;
    headerview.layer.masksToBounds = YES;
    
    lineA.constant = LineHeight;
    lineB.constant = LineHeight;
    
    tableViewHaderView.frame = CGRectMake(0, 0, ScreenWidth, 208);
    mainTableView.tableHeaderView = tableViewHaderView;
    
    [self updateMoney:nil];
}

- (void)updateMoney:(NSString *)money {

    [[Userinfo standardUserinfo] updateMoney:money];
    
    if (money.length == 0) {
        money = @"--";
    } else {
        money = [NSString stringWithFormat:@"%@元",money];
    }

    NSString *strA = @"可用余额:";
    
    NSString *attrStr = [NSString stringWithFormat:@"%@%@",strA,money];
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:attrStr];
    [str addAttribute:NSForegroundColorAttributeName value:Color333333 range:NSMakeRange(0,5)];
    [str addAttribute:NSForegroundColorAttributeName value:COLOR_FF5253 range:NSMakeRange(5, attrStr.length - 5)];
    
    [str addAttribute:NSFontAttributeName value:[MTool fontOfSize:12] range:NSMakeRange(0, 5)];
    [str addAttribute:NSFontAttributeName value:[MTool fontOfSize:18] range:NSMakeRange(5, attrStr.length - 5)];
    
    moneyLabel.attributedText = str;
    moneyLabel.textAlignment = NSTextAlignmentCenter;
    moneyLabel.numberOfLines = 0;
}

@end













